<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>16-登录成功跳转至首页</title>
	</head>
	<body>
		<form method="post">
			<label for="">用户名：<input type="text" id='username' placeholder='请输入用户名'></label>
			<label for="">密码：<input type="password" id='password' placeholder='请输入密码'></label>
			<button>登录</button>
		</form>
		<script type="text/javascript" src="js/jquery2.1.4.js"></script>
		<script type="text/javascript">
			//获取按钮
			var btn = document.querySelector('button');
			var userobj = document.querySelector('#username');
			var passobj = document.querySelector('#password');
			btn.onclick = function(e){
				//1.数据校验
				if(userobj.value==''
				||userobj.length==0
				||userobj.value==undefined
				||userobj.value==null){
					alert('请输入用户名!');
					return;
				}
				if(passobj.value==''
				||passobj.length==0
				||passobj.value==undefined
				||passobj.value==null){
					alert('请输入密码!');
					return;
				}				
				//2.交互数据
				//alert('交互数据');
				$.ajax({
					url:'https://cache.iminutes.cn/api/soft/test.html',
					type:'POST',
					data:{
						username:userobj.value,
						password:passobj.value
					},
					dataType:'json',
					success:function(res){
						console.log(res);
						if(res.status==200){
							alert(res.msg);
							//让它3秒之后跳转至首页
							setTimeout(function(){
								window.location.href='index.html';
							},3000);
						}else{
							alert(res.msg);
						}
					}
					
				});
				e.preventDefault();
			}
		</script>
	</body>
</html>
